package com.hustbaidu.hustmap;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ScoreListActivity extends Activity {
	private TextView scorePlaceNameWidget;
	private EditText scoreEditWidget;
	private ImageButton scoreSubmitWidget;
	private ListView scoreListWidget;
	
	private int placeId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.score_list_layout);
		
		PlaceInfo place = getIntent().getExtras().getParcelable("place");
		placeId = place.getId();
		
		scorePlaceNameWidget = (TextView)findViewById(R.id.score_place_name);
		scorePlaceNameWidget.setText("地点:" + place.getPlaceName());
		
		scoreEditWidget = (EditText)findViewById(R.id.score_edit);
		
		scoreSubmitWidget = (ImageButton)findViewById(R.id.score_submit);
		scoreSubmitWidget.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		scoreListWidget = (ListView)findViewById(R.id.score_list);
		SimpleAdapter adapter = new SimpleAdapter(this, getScoreListData(), R.layout.score_list_item, 
				new String[]{"score_item_content", "score_item_user_name", "score_item_time"}, 
				new int[]{R.id.score_item_content, R.id.score_item_user_name, R.id.score_item_time});
		scoreListWidget.setAdapter(adapter);
		
	}
	
	public List<Map<String, Object>> getScoreListData(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		List<Score> scores = ScoreManager.getAllScores(placeId);
		Map<String, Object> map = null;
		Score score = null;
		
		for(int i = 0; i < scores.size(); i++){
			score = scores.get(i);
			
			map = new HashMap<String, Object>();
			map.put("score_item_content", score.getContent());
			map.put("score_item_user_name", score.getUserName());
			
			Timestamp time = score.getTime();
			SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
			
			map.put("score_item_time", format.format(time));
			
			list.add(map);
		}
		
		return list;
	}

}
